package com.entities.usergroup.detail;


import java.util.List;

import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IDescriptionViewDelegate;
import com.core.protocols.IDetailTableViewDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.protocols.ITabSheetDatasource;
import com.core.ui.controller.base.HibernateDetailController;
import com.core.ui.customcell.TabSheetItemCell;
import com.core.utils.LocalizableString;
import com.entities.usergroup.servicelayer.UserGroupListLayer;
import com.model.user.UserGroup;


public class UserGroupDetailController
        extends HibernateDetailController
        implements IDetailTableViewDelegate, IDescriptionViewDelegate, IServiceLayerDelegate, ITabSheetDatasource {


	private String             groupId;
	private UserGroupListLayer groupListLayer = null;
	private UserGroup          groupItem;



	public UserGroupDetailController() {

		groupListLayer = new UserGroupListLayer();

	}



	public String getGroupId () {

		return groupId;
	}



	public void setGroupId ( String groupId ) {

		this.groupId = groupId;
	}



	/**
	 * ICustomDatasource
	 * 
	 * 
	 */


	@Override
	public void reloadData () {


		Integer result = null;
		try {
			result = Integer.parseInt(this.groupId);
		} catch ( NumberFormatException e ) {
			result = null;
		}


		groupItem = (UserGroup) groupListLayer.objectByUNID(result, null, this);

	}



	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		// TODO Auto-generated method stub

	}



	/**
     *
     *
     *
     *
     */


	@Override
	public int tabSheetCellsCount () {

		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	public TabSheetItemCell tabSheetCellAtIndex ( int index ) {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String tabsPanelStyleClassName () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String descriptionData () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public int detailTableViewItemsCount ( String userInfo ) {

		int result = 0;
		String userInfoUC = userInfo.toUpperCase();
		if ( userInfoUC.equals("BASE") ) {
			result = 2;
		}
		return result;
	}



	@Override
	public String detailTableViewItemAtIndex ( String userInfo,
	                                           int index ) {

		String result = null;

		if ( userInfo.toUpperCase().equals("BASE") ) {
			result = this.groupBaseItemAtIndex(index);
		}
		return result;
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		if ( request == null ) {
			return;
		}

	}



	private String groupBaseItemAtIndex ( int index ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		String result = null;

		String style = null;
		String propertyValue = null;
		String data = null;

		boolean readOnly = this.isReadOnly();

		switch ( index ) {
			case 0: {
				style = "-single";
				if ( groupItem != null ) {
					propertyValue = groupItem.getName();
				}
				String caption = localizableString.localizable("entity.UserGroup.field.name");
				data = this.inputControl("name", caption, propertyValue, readOnly);
				break;
			}
			case 1: {
				style = "-single";
				if ( groupItem != null ) {
					propertyValue = groupItem.getDescription();
				}
				String caption = localizableString.localizable("entity.UserGroup.field.description");
				data = this.textAreaControl("description", caption, propertyValue, readOnly);
				break;
			}
			default: {
				break;
			}
		}
		result = String.format("<li class=\"property-item%s\"><div class=\"wrap\">%s</li>", style, data);
		return result;
	}



}
